package com.milhouz.quizzdroid.bean;

import android.database.Cursor;
import android.os.Parcel;
import android.os.Parcelable;

import com.milhouz.quizzdroid.db.QuizzDroidConstants;
import com.milhouz.quizzdroid.exception.DatabaseRequestException;

public class Question implements Parcelable {

	private int mId;
	private String mQuestion;
	private String mResponse;
	private String mWikiLink;
	private Category mCategory;
	private int mCategoryId;
	private ResponseList mResponses;

	public Question() {

	}

	public Question(final Cursor cursor) throws DatabaseRequestException {
		if (cursor != null && cursor.moveToFirst()) {
			mId = cursor.getInt(cursor.getColumnIndex(QuizzDroidConstants.ID));
			mQuestion = cursor.getString(cursor.getColumnIndex(QuizzDroidConstants.COLUMN_NAME_QUESTION));
			mResponse = cursor.getString(cursor.getColumnIndex(QuizzDroidConstants.COLUMN_NAME_RESPONSE));
			mWikiLink = cursor.getString(cursor.getColumnIndex(QuizzDroidConstants.COLUMN_NAME_WIKI_LINK));
			mCategoryId = cursor.getInt(cursor.getColumnIndex(QuizzDroidConstants.COLUMN_NAME_CATEGORY_ID));
		} else {
			throw new DatabaseRequestException("Question cursor null or empty");
		}
	}

	public int getId() {
		return mId;
	}

	public String getQuestion() {
		return mQuestion;
	}

	public String getResponse() {
		return mResponse;
	}

	public int getCategoryId() {
		return mCategoryId;
	}

	public Category getCategory() {
		return mCategory;
	}

	public void setCategory(final Category mCategory) {
		this.mCategory = mCategory;
	}

	public void setResponses(ResponseList mResponses) {
		this.mResponses = mResponses;
	}

	public ResponseList getResponses() {
		return mResponses;
	}

	public String getWikiLink() {
		return mWikiLink;
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel parcel, int flag) {
		parcel.writeInt(mId);
		parcel.writeString(mQuestion);
		parcel.writeInt(mCategoryId);
		parcel.writeParcelable(mCategory, flag);
		parcel.writeParcelable(mResponses, flag);
		parcel.writeString(mWikiLink);
	}

	public static final Creator<Question> CREATOR = new Creator<Question>() {
		@Override
		public Question createFromParcel(Parcel source) {
			Question q = new Question();
			q.mId = source.readInt();
			q.mQuestion = source.readString();
			q.mCategoryId = source.readInt();
			q.mCategory = (Category) source.readParcelable(Category.class.getClassLoader());
			q.mResponses = (ResponseList) source.readParcelable(ResponseList.class.getClassLoader());
			q.mWikiLink = source.readString();
			return q;
		}

		@Override
		public Question[] newArray(int arg0) {
			return null;
		}
	};

}
